package 数组题目.前缀和与差分;

//区域和检索 - 数组不可变 - 基础前缀和
//https://leetcode.cn/problems/range-sum-query-immutable/description/
class 一维前缀和 {

    private int[] numsSum;

    public 一维前缀和(int[] nums) {
        //-2 -2 1 -4 -2 -3
        numsSum = new int[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            this.numsSum[i + 1] = numsSum[i] + nums[i];
        }
    }

    public int sumRange(int left, int right) {
        return numsSum[right + 1] - numsSum[left];
    }

    public static void main(String[] args) {
        一维前缀和 一维前缀和 = new 一维前缀和(new int[]{-2, 0, 3, -5, 2, -1});
        System.out.println(一维前缀和.sumRange(0, 2));
        System.out.println(一维前缀和.sumRange(1, 3));
        System.out.println(一维前缀和.sumRange(2, 4));
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(left,right);
 */
